package cn.biplam.front.htmlscript.script;

import cn.biplam.common.ajxcode.MdCode.BmMangeCode;
import cn.biplam.common.ajxcode.MdCode.DwMangeCode;
import cn.biplam.common.ajxcode.MdCode.SrcMangeCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.TreeIndexCode;
import cn.biplam.common.paramters.ModelType.Model_Type;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.common.wrapper.tree.TreeIndexFields;
import cn.biplam.front.component.data.mdManage.MdObjectSelect;
import cn.biplam.front.component.data.tree.OptTreeRecordId;
import cn.biplam.front.component.view.mdManage.ModelTree;

public class ScriptTree {
	
	public static String tbName="model_index";
	
	/**
	 * 树的刷新脚本(单棵树)
	 * @param pcid            调用树的Vc的容器id
	 * @param actionId        生成树的vc的行为id
	 * @param extendNodeId    需要展开的节点id
	 * @param extendNodeLevel 需要展开的节点级别
	 * @param nodeType        节点的类型
	 * @return 数的刷新脚本(单棵树)
	 */
	public static String getTreeRefresh(String pcid,
										String actionId,
										String extendNodeId,
										String extendNodeLevel,
										String nodeType){
		return getTreeRefresh(pcid, actionId, extendNodeId, extendNodeLevel, nodeType, null);
	}
	
	/**
	 * 树的刷新脚本(多棵树的刷新)
	 * @param pcid            调用树的Vc的容器id
	 * @param actionId        生成树的vc的行为id
	 * @param extendNodeId    需要展开的节点id
	 * @param extendNodeLevel 需要展开的节点级别
	 * @param nodeType        节点的类型
	 * @param allNodeType     所有的类型,用"_"分割。如1_2_3
	 * @Param otherParam      其他参数"paramName,paramValue"
	 * @return 数的刷新脚本(多棵树)
	 */
	public static String getTreeRefresh(String pcid,
										String actionId,
										String extendNodeId,
										String extendNodeLevel,
										String nodeType,
										String allNodeType,
										String ... otherParam){
		StringBuilder sb = new StringBuilder();
		sb.append("treeWholeRefresh(");
		sb.append("'"+TOOLS.stringClip(pcid, 4)+"',");
		sb.append("'"+actionId+"',");
		sb.append("'"+extendNodeId+"',");
		sb.append("'"+extendNodeLevel+"',");
		sb.append("'"+nodeType+"',");
		if(allNodeType==null){
			allNodeType = nodeType;
		}
		sb.append("'"+allNodeType+"'");
		if(otherParam==null){
			otherParam = new String[]{"test,test"};
		}
		for(int i=0;i<otherParam.length;i++){
			sb.append(",'");
			sb.append(otherParam[i]);
			sb.append("'");
		}
		
		sb.append(");");
		return sb.toString();
	}
	
	/**
	 * 模型对象选择树的刷新脚本
	 * @param node 树右键菜单生成的记录ID
	 * @return 模型对象选择树的刷新脚本
	 */
	public static String getModelMoveTreeRefresh(String node){
		String[] nodeInfo=node.split(",");
		return ScriptTree.getTreeRefresh(MdObjectSelect.cId, ModelTree.aId, nodeInfo[0], nodeInfo[2], nodeInfo[3]);
	}
	
	/**
	 * 获取业务模型管理导航栏的修改对象操作后刷新树的脚本，传入树右键菜单生成的记录ID
	 * @param node 树右键菜单生成的记录ID
	 * @return 业务模型管理导航栏的修改对象操作后刷新树的脚本
	 * @throws ErrorMessage 
	 */
	public static String getModelTreeEditRefresh(String node) throws ErrorMessage{
		OptTreeRecordId nodeInfo = new OptTreeRecordId(node);
		return getTreeRefresh(nodeInfo.getIndexNodeID(),  nodeInfo.getIndexNodeType());
	}
	
	/**
	 *  获取业务模型管理导航栏的添加对象操作后刷新树的脚本，传入树右键菜单生成的记录ID
	 * @param node 树右键菜单生成的记录ID
	 * @param nodeID 新插入节点的物理表ID
	 * @param userID 用户ID
	 * @return 业务模型管理导航栏的添加对象操作后刷新树的脚本
	 * @throws ErrorMessage 
	 */
	public static String getModelTreeAddRefresh(String node,String nodeID,String userID) throws ErrorMessage{
//		OptTreeRecordId nodeInfo = new OptTreeRecordId(node);
//		String nodeLevel=(TOOLS.stringToInt(nodeInfo.getIndexNodeLevel())+1)+"";
		TreeIndexFields fields=TreeIndexFields.getTreeFields(tbName);
		DataFilter filter=new DataFilter();
		filter.put("table_name", tbName);
		filter.put(fields.getRealId(), nodeID);
		SqlResult sr=Operator.get(TreeIndexCode.Select_By_Rel_ID, userID, filter).getSelectResult();
		if(sr!=null&&!sr.isNull()){
			String treeNodeId=sr.get(0, fields.getNodeId());
			String nodeType=sr.get(0, fields.getNodeType());
			return getTreeRefresh(treeNodeId, nodeType);
		}else{
			return null;
		}
	}
	
	/**
	 *  获取业务模型管理导航栏的删除对象操作后刷新树的脚本，传入树右键菜单生成的记录ID
	 * @param node 树右键菜单生成的记录ID
	 * @param userID 用户ID
	 * @return 业务模型管理导航栏的删除对象操作后刷新树的脚本
	 * @throws ErrorMessage
	 */
	public static String getModelTreeDelRefresh(String node,String userID) throws ErrorMessage{
		OptTreeRecordId nodeInfo = new OptTreeRecordId(node);
		DataFilter filter=new DataFilter();
		TreeIndexFields fields=TreeIndexFields.getTreeFields(tbName);
		filter.put("table_name", tbName);
		filter.put(fields.getNodeId(), nodeInfo.getIndexNodeID());
		filter.put(fields.getNodeLevel(), nodeInfo.getIndexNodeLevel());
		OperatorResult sr=Operator.get(TreeIndexCode.Get_Nearest_Node, userID, filter);
		if(sr!=null&&!sr.isNull()){
			String nodeID=sr.getValue(fields.getNodeId());
			String nodeType=sr.getValue(fields.getNodeType());
			return getTreeRefresh(nodeID, nodeType);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据模型类型生成刷新树的代码 
	 * @return
	 */
	public static String getTreeRefresh(String nodeID, String nodeType){
		if(nodeType.equals(Model_Type.Dw.toString())){
			return ScriptTree.getTreeRefresh(DwMangeCode.cId, ModelTree.aId, nodeID, "", nodeType);
		}else if(nodeType.equals(Model_Type.Src.toString())){
			return ScriptTree.getTreeRefresh(SrcMangeCode.cId, ModelTree.aId, nodeID, "", nodeType);
		}else if(nodeType.equals(Model_Type.Bm.toString())){
			return ScriptTree.getTreeRefresh(BmMangeCode.cId, ModelTree.aId, nodeID, "", nodeType);
		}else{
			return null;
		}
	}
}
